package com.sugon.controller.forecase;

import com.sugon.dao.DevParameterCDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class SensorReport {
    @Autowired
    DevParameterCDao devParameterCDao;
    @Autowired
    RestTemplate restTemplate;
    @GetMapping("SensorReport/getParm")
    public List<Map> getParm(String device){
        return devParameterCDao.getParmName(device);
    }
    @PostMapping("SensorReport/getPramDatas")
    public List<Map> getSensorsParm(@RequestBody Map<String, Object> params){
        return devParameterCDao.getDatas(params);
    }
    public Map getSensorsParm2Backup(@RequestBody Map<String, Object> params){
        if(!params.containsKey("items") || !(params.get("items") instanceof ArrayList)){
            return null;
        }
        Map result = new HashMap();
        ArrayList<String> arrayList = (ArrayList) params.get("items");
        for (String item:arrayList) {
            List<Map> res = devParameterCDao.getDatas2(params,item);
            result.put(item,res);
        }

        return result;
    }
    @PostMapping("cx/saveAeff")
    public String saveAeff(@RequestBody Map params){
        List<Map> datas = (List<Map>) params.get("datas");
        String devcode = (String) params.get("Devcode");
        if(datas != null && datas.size() > 0){
            devParameterCDao.saveAeff(devcode,datas);
            Long lasttime = devParameterCDao.getLasttime((String) params.get("Devcode"),"Aeff");
            Map last =datas.get(datas.size() - 1);
            if(lasttime == null){
                devParameterCDao.saveLasttime((Long) last.get("DateTime"),devcode,"Aeff");
            }else{
                devParameterCDao.updateLasttime((Long) last.get("DateTime"),devcode,"Aeff");
            }
        }
        return "ok";
    }
    @PostMapping("cx/saveVeff")
    public String saveVeff(@RequestBody Map params){
        List<Map> datas = (List<Map>) params.get("datas");
        String devcode = (String) params.get("Devcode");
        if(datas != null && datas.size() > 0) {
            devParameterCDao.saveVeff(devcode,datas);
            Long lasttime = devParameterCDao.getLasttime((String) params.get("Devcode"), "Veff");
            Map last = datas.get(datas.size() - 1);
            if (lasttime == null) {
                devParameterCDao.saveLasttime((Long) last.get("DateTime"), devcode, "Veff");
            } else {
                devParameterCDao.updateLasttime((Long) last.get("DateTime"), devcode, "Veff");
            }
        }
        return "ok";
    }
    @PostMapping("cx/saveUnbalance")
    public String saveUnbalance(@RequestBody Map params){
        List<Map> datas = (List<Map>) params.get("datas");
        String devcode = (String) params.get("Devcode");
        if(datas != null && datas.size() > 0) {
            devParameterCDao.saveDatas(devcode, datas, "DEV_SENSOR_UNBALANCE");
            Long lasttime = devParameterCDao.getLasttime((String) params.get("Devcode"), "Unbalance");
            Map last = datas.get(datas.size() - 1);
            if (lasttime == null) {
                devParameterCDao.saveLasttime((Long) last.get("DateTime"), devcode, "Unbalance");
            } else {
                devParameterCDao.updateLasttime((Long) last.get("DateTime"), devcode, "Unbalance");
            }
        }
        return "ok";
    }
    @PostMapping("cx/savePeak2")
    public String savePeak2(@RequestBody Map params){
        List<Map> datas = (List<Map>) params.get("datas");
        String devcode = (String) params.get("Devcode");
        if(datas != null && datas.size() > 0) {
            devParameterCDao.saveDatas(devcode, datas, "DEV_SENSOR_PEAK2");
            Long lasttime = devParameterCDao.getLasttime((String) params.get("Devcode"), "Peak2");
            Map last = datas.get(datas.size() - 1);
            if (lasttime == null) {
                devParameterCDao.saveLasttime((Long) last.get("DateTime"), devcode, "Peak2");
            } else {
                devParameterCDao.updateLasttime((Long) last.get("DateTime"), devcode, "Peak2");
            }
        }
        return "ok";
    }
    @PostMapping("cx/savePeak1")
    public String savePeak1(@RequestBody Map params){
        List<Map> datas = (List<Map>) params.get("datas");
        String devcode = (String) params.get("Devcode");
        if(datas != null && datas.size() > 0) {
            devParameterCDao.saveDatas(devcode, datas, "DEV_SENSOR_PEAK1");
            Long lasttime = devParameterCDao.getLasttime((String) params.get("Devcode"), "Peak1");
            Map last = datas.get(datas.size() - 1);
            if (lasttime == null) {
                devParameterCDao.saveLasttime((Long) last.get("DateTime"), devcode, "Peak1");
            } else {
                devParameterCDao.updateLasttime((Long) last.get("DateTime"), devcode, "Peak1");
            }
        }
        return "ok";
    }
    @PostMapping("SensorReport/getPramDatas2")
    public Map getSensorsParm2(@RequestBody Map<String, Object> params){
        String url ="http://127.0.0.1:7002/getDatas";
        ResponseEntity<Map> responseEntity = restTemplate.postForEntity(url,params, Map.class);
        Map result = responseEntity.getBody();
        return result;
    }
    @PostMapping("cx/getLasttime")
    public long getLasttime(@RequestBody Map params){
        String devcode = (String) params.get("Devcode");
        String field = (String) params.get("field");
        Long lasttime = devParameterCDao.getLasttime(devcode,field);
        if(lasttime == null){
            return System.currentTimeMillis() - 1000;
        }else{
            return lasttime;
        }
    }
}
